package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppPackageBo;
import com.ruoyi.app.domain.vo.AppPackageVo;
import com.ruoyi.app.domain.AppPackage;
import com.ruoyi.app.mapper.AppPackageMapper;
import com.ruoyi.app.service.IAppPackageService;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * APP套餐Service业务层处理
 *
 * @author ruoyi
 * @date 2025-06-21
 */
@RequiredArgsConstructor
@Service
public class AppPackageServiceImpl implements IAppPackageService {

    private final AppPackageMapper baseMapper;

    /**
     * 查询APP套餐
     */
    @Override
    public AppPackageVo queryById(Long packageId){
        return baseMapper.selectVoById(packageId);
    }

    /**
     * 查询APP套餐列表
     */
    @Override
    public TableDataInfo<AppPackageVo> queryPageList(AppPackageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppPackage> lqw = buildQueryWrapper(bo);
        Page<AppPackageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        //查看自己是否有过期的包 如果有则设置过期


        return TableDataInfo.build(result);
    }

    /**
     * 查询APP套餐列表
     */
    @Override
    public List<AppPackageVo> queryList(AppPackageBo bo) {
        LambdaQueryWrapper<AppPackage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<AppPackage> buildQueryWrapper(AppPackageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppPackage> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getPackageName()), AppPackage::getPackageName, bo.getPackageName());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AppPackage::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增APP套餐
     */
    @Override
    public Boolean insertByBo(AppPackageBo bo) {
        AppPackage add = BeanUtil.toBean(bo, AppPackage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setPackageId(add.getPackageId());
        }
        return flag;
    }

    /**
     * 修改APP套餐
     */
    @Override
    public Boolean updateByBo(AppPackageBo bo) {
        AppPackage update = BeanUtil.toBean(bo, AppPackage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppPackage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除APP套餐
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public AppPackage selByPrice(BigDecimal amount) {
        LambdaQueryWrapper<AppPackage> appPackageLambdaQueryWrapper = new LambdaQueryWrapper<>();
        appPackageLambdaQueryWrapper.le(AppPackage::getPackagePrice, amount);
        return baseMapper.selectOne(appPackageLambdaQueryWrapper);
    }
}
